﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using LuxuryResort.WebSite.LRMPDataService;

namespace LuxuryResort.WebSite.Members
{
    public partial class FoodBeverageList : Page
    {
        private readonly RoomServiceClient _svc = new RoomServiceClient();

        private Guid UserID
        {
            get
            {
                if (ViewState["UserID"] == null && HttpContext.Current.User != null)
                {
                    string userName = HttpContext.Current.User.Identity.Name;
                    object userId = Membership.GetUser(userName).ProviderUserKey;
                    ViewState["UserID"] = userId;
                }
                return (Guid) ViewState["UserID"];
            }
        }

        private Guid ResortId
        {
            get { return new Guid(ConfigurationManager.AppSettings["ResortId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void DataPagerFoods_PreRender(object sender, EventArgs e)
        {
            try
            {
                lvFood.DataSource = _svc.GetAllFood(UserID, ResortId);
                lvFood.DataBind();
            }
            catch (Exception ex)
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var item = new FoodBeverage
            {
                FID = new Guid(hidFoodID.Value),
                Name = txtFoodName.Text,
                Type = ddlFoodTypes.SelectedValue,
                PriceUnit = Convert.ToDecimal(txtPrice.Text)
            };

            int i = _svc.UpdateFood(item, UserID, ResortId);

            if (i > 0)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "showMessage",
                    string.Format("showMessage('{0}')", "Update successfully !!!"), true);
            }
        }

        protected void lvFood_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "UpdateFood")
            {
                BindDataToFood(e.Item);
            }
            if (e.CommandName == "DeleteFood")
            {
                string hidFoodId = ((HiddenField) e.Item.FindControl("hidFid")).Value;
                _svc.DeleteFood(new Guid(hidFoodId), UserID, ResortId);
            }
        }

        private void BindDataToFood(ListViewItem item)
        {
            btnAddItem.Visible = false;
            btnUpdate.Visible = true;

            hidFoodID.Value = ((HiddenField) item.FindControl("hidFid")).Value;
            txtFoodName.Text = ((Label) item.FindControl("lblItemName")).Text;
            ddlFoodTypes.SelectedValue = ((Label) item.FindControl("lblType")).Text;
            txtPrice.Text = ((Label) item.FindControl("lblPrice")).Text;

            divFoodInformation.Visible = true;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            divFoodInformation.Visible = true;
            btnUpdate.Visible = false;
            btnAddItem.Visible = true;

            txtFoodName.Text = string.Empty;
            txtPrice.Text = string.Empty;
        }

        protected void btnAddItem_Click(object sender, EventArgs e)
        {
            var foodBeverage = new FoodBeverage
            {
                FID = Guid.NewGuid(),
                Name = txtFoodName.Text,
                Type = ddlFoodTypes.SelectedValue,
                PriceUnit = !string.IsNullOrEmpty(txtPrice.Text) ? Convert.ToDecimal(txtPrice.Text) : 0
            };

            int i = _svc.InsertFood(foodBeverage, UserID, ResortId);

            if (i > 0)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "showMessage",
                    string.Format("showMessage('{0}')", "Insert successfully !!!"), true);
            }
        }
    }
}